<?php
// app/Controller/UsersController.php
class UsersController extends AppController
{

    var $uses = array(
        'User'
    );

    # Set the layout to use
    var $layout = 'login_layout';

    public function beforeFilter()
    {
        parent::beforeFilter();
        //$this->Security->blackHoleCallback = 'my_blackhole';
        $this->Auth->allow('login', 'logout', 'addtemp');
    }

    public function login()
    {
        if ($this->request->is('post')) {
            $success = true;
            $result = $this->Auth->login();
            debug($this->Auth->authError);
            debug($result);
            //exit;
            if ($result) {
                /** Check if account is disabled */
                $active = $this->Auth->user('active');
                if ($active == 'n') {
                    /** User is disable, Logout */
                    $success = false;
                    $this->Auth->logout();
                    $this->Session->setFlash(__('Your Account is Disabled.'));
                }
            } else {
                $success = false;
                $this->Session->setFlash(__('Invalid login Credentials'));
            }

            if ($success) { //Login Successful
                $this->redirect(array('controller' => 'Student', 'action' => 'index'));
            }
        }

        $this->layout = 'login_layout';
    }


    public function logout()
    {
        $this->redirect($this->Auth->logout());
    }


    function action_after_login()
    {
        $this->redirect(array('controller' => 'Dashboard', 'action' => 'index'));
    }

    public function addtemp()
    {
        if ($this->request->is('post')) {
            echo debug($this->request->data);
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                echo 'The user has been saved';
                $this->redirect(array('action' => 'index'));
            } else {
                echo 'The user could not be saved. Please, try again.';
            }
        }
    }

}